//
// Created by bbnf on 09.06.2024.
//
#include "storage/todo.h"

namespace TodoApp::Storage {
    Todo::Todo(std::string title, std::string description)
            : m_title(std::move(title)), m_description(std::move(description)), m_done(false), m_priority(Todo::Low) {
    }

    void Todo::switchDone() {
        setDone(!m_done);
    }

    void Todo::setPriority(Todo::Priority priority) {
        m_priority = priority;
    }

    void Todo::setTitle(std::string title) {
        m_title = std::move(title);
    }

    void Todo::setDescription(std::string description) {
        m_description = std::move(description);
    }

    auto Todo::endDate(std::string format) -> std::string {
        // TODO: Найти способ формата даты и времени
        std::stringstream ss;
        ss << std::put_time(std::localtime(&m_done_time), format.c_str());
        return ss.str();
    }

    void Todo::setDone(const bool &done) {
        if (done) {
            m_done_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        }
        m_done = done;
    }


    void Todo::setDoneTime(const std::int64_t &time) {
        m_done_time = time;
    }

    void Todo::setPriority(const int8_t &priority) {
        m_priority = static_cast<Priority>(priority);
    }

    auto Todo::serialize() const -> std::string {
        return std::format("{}||{}||{}||{}||{}", m_done, m_title, m_description, static_cast<std::int8_t >(m_priority),
                           m_done_time);
    }

    auto operator<<(std::ostream &os, const Todo &todo) -> std::ostream & {
        return os << todo.serialize();
    }






}  // namespace TodoApp::Storage
